/*************************************************
 *FileName:      myfriend.js
 *Description:   我的好友.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    /*
     * 变量初始化
     */
    var that =getUrlParam('that');
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var ohtml = '';//页面数据加载
    /*
     * HTML5+准备完成
     */
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    window.addEventListener('newfriend', function(e){
        myfriend();
    });
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
          pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        var onav ='';
        for(var i=0;i<26;i++){
            onav += '<a data-id="'+String.fromCharCode(97+i)+'">'+String.fromCharCode(65+i)+'</a>'
        }
        $('.nav .top').after(onav);//字母输出
        
        if(that=='shield'){
            $('.mui-title').text('已屏蔽朋友圈');
            mui.plusReady(myshield)
        }else if(that=='phone'){
            $('.mui-title').text('已屏蔽号码');
            mui.plusReady(myphone)
        }else if(that=='chat'){
            $('.mui-title').text('选择好友');
            mui.plusReady(myfriend)
        }else{ 
            $('.mui-title').text('我的好友');
            mui.plusReady(myfriend)
            $('.main').on('tap','.mf-table-view-cell',function(){
                common.pageTransition('chat.html?thatid='+$(this).attr('data-id'))
            })
        }
        
        $('.nav a').on('tap', function() {
            var oheader = $('header.mui-bar').height() + $('.serch').height() + $('.title').height();
            var obj = '#' + $(this).attr('data-id');
            if(!$('.main').find($(obj)).length){return false;}
            var distance = -common.getTop($(obj)[0]) //计算高度
            var osum = distance + oheader;
            mui('.mui-scroll-wrapper').scroll().scrollTo(0, osum, 100);
        }) //字母跳转
        
        $('.myfriend').on('swipeleft','.mf-table-view-cell',function(){
            $(this).css({transform:'translateX(-20%)'});
        })
        $('.myfriend').on('swiperight','.mf-table-view-cell',function(){
            $(this).css({transform:'translateX(0%)'});
        })
        
        $('.main').on('tap','.delect',function(){
            var othis =$(this).parents('.mf-table-view-cell');
            delectfringd(othis)
            return false;
        })
        
        $('.main').on('tap','.userimg',function(){
            var ofuid =$(this).parents('.mf-table-view-cell').attr('data-id');
            common.pageTransition('personal.html?friend_uid='+ofuid)
            return false;
        })
        
        $('.myfriend').on('tap','.show',function(){
            var othat =$(this);
            var oname =$(this).parents('.shield').find('.user').text();
            mui.confirm('确定显示'+oname+'的朋友圈','提示',['确定','取消'],function(e){
                if(!e.index){
                    dershield(othat);
                }
            },'div');
        })
        
        $('.myfriend').on('tap','.cancel',function(){
            var othat =$(this);
            var ophone =$(this).parents('.userphone').find('.phone').text();
            mui.confirm('确定取消号码'+ophone+'的屏蔽','提示',['确定','取消'],function(e){
                if(!e.index){
                    derphone(othat);
                }
            },'div');
        })
        
        $('.mui-scroll-wrapper').on('tap',function(){
            $('input').blur()
        })
        $('.serch-input').focusin(function(){
            $('.title').css({display:'none'});
            $('.fold').css({display:'none'});
            if(!$(this).val()){ $('.main a').css({display:'none'})}
            $('nav.nav').css({display:'none'});
        }).on('input',function(){
            $('.main a').css({display:'none'})
            if($(this).val()){
                for(var i =0 ; i<$('.main a').length ;i++){
                    otext = $(this).val();
                    Cts = $('.main a').eq(i).find('.user').text()+$('.main a').eq(i).find('.phone').text();
                    Cts.indexOf(otext) > -1 ? $('.main a').eq(i).css({display:'-webkit-flex'}) : false ;
                }
            }
        }).focusout(function(){
            if(!$(this).val()){
                $('.title').css({display:'block'});
                $('.fold').css({display:'block'});
                $('.main a').css({display:'-webkit-flex'});
                $('nav.nav').css({display:'block'});
            }
        })//搜索效果
        
        var osetfurl ,osetfname , ousname ,ousid;
        $('.myfriend').on('tap','.setfriend',function(){
            osetfurl = $(this).find('img').attr('src');
            osetfname =localStorage.getItem("othername");
            ousname =$(this).find('.user').text();
            ousid=$(this).attr('data-id');
            $('.osetfname').text(osetfname);
            $('.ousname').text(ousname);
            $('.osetfurl').attr('src',osetfurl);
            $('.ousid').text('ID号：'+ousid);
            $('.card-box').css({display:'block'});
            
        })//返回聊天页面
        
        if(that=='chat'){
            $('.myfriend').on('tap','.card-box .mui-ok',function(){
                localStorage.setItem('sharename',ousname)
                localStorage.setItem('shareurl',osetfurl)
                localStorage.setItem('shareid',ousid)
                mui.back();
                var list = plus.webview.currentWebview().opener();
                mui.fire(list, 'chatcard');//返回true,继续页面关闭逻辑
                return true;
            })
        }
        
             
        
        $('.nav .top').on('tap',function(){
            mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);//100毫秒滚动到顶
        })
        $('.nav .bottom').on('tap',function(){
            mui('.mui-scroll-wrapper').scroll().scrollToBottom(0,0,100);//100毫秒滚动到顶
        })
        
        $('.card-box .mui-popup-button-bold').on('tap',function(){
            $('.card-box').css({display:'none'})
        })
        
        $('.main').on('tap','#newfriend',function(){
            common.pageTransition('newfriend.html')
        })
        
        
    }
//获取我的好友用户信息
function myfriend() {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/friend/getFriendsList",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token
        },
        success: function(data) {
            if(data.error_code == 0) {
                if(that=='chat'){
                    for(var i = 0; i < data.data.length; i++) {
                        var su,sn = data.data[i].text;
                        data.data[i].text == '' ?su = '#':su = data.data[i].text;
                        ohtml += '<a class="title" id="' + sn.toLowerCase() + '">' + su + '</a>';
                        for(var j = 0; j < data.data[i].data.length; j++) {
                            ohtml += '<a class="setfriend" data-id="' + data.data[i].data[j].uid + '">'
                                  +'<img class="img" src="' + DOMAIN_NAME+data.data[i].data[j].face + '" onerror="imgExists(this)" />'
                                  +'<div class="user">' + data.data[i].data[j].nickname + '</div></a>';
                        }
                    }
                }else{
                    data.apply ? ohtml+='<div class="fold"><a class="fold-a"id="newfriend"><div class="fold-title">有新朋友</div>有<span>'+data.apply+'</span>个人请求添加你为好友<i class="mui-icon mui-icon-arrowright"></i></a></div>': false;
                    for(var i = 0; i < data.data.length; i++) {
                        var su,sn = data.data[i].text;
                        data.data[i].text == '' ?su = '#':su = data.data[i].text;
                        ohtml += '<a class="title" id="' + sn.toLowerCase() + '">' + su + '</a>';
                        for(var j = 0; j < data.data[i].data.length; j++) {
                            ohtml += '<a class="mf-table-view-cell" data-id="' + data.data[i].data[j].uid + '"><div class="userimg">';
                            data.data[i].data[j].is_new ? ohtml += '<em class="new"></em>' : ohtml += '';
                            ohtml += '<img src="' +DOMAIN_NAME+ data.data[i].data[j].face + '" onerror="imgExists(this)" />' +
                                '</div><div class="mf-navigate-right">' +
                                '<div class="user">' + data.data[i].data[j].nickname + '</div><div class="age">';
                            data.data[i].data[j].sex == 'woman' ? ohtml += '<img src="images/icon_girl.png"/>' : ohtml += '<img src="images/icon_boy.png"/>';
                            ohtml += '<span>' + data.data[i].data[j].age + '</span></div>';
                            data.data[i].data[j].level ? ohtml += '<div class="age sex"><span>' + data.data[i].data[j].level + '级魅力</span></div></div><i class="mui-icon mui-icon-arrowright arrows"></i><div class="delect"></div></a>' : ohtml += '</div><i class="mui-icon mui-icon-arrowright arrows"></i><div class="delect"></div></a>';
                        }
                    }
                }
                $('.main').html(ohtml);
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                    exitlogin();
                },1000);
            } else {
                if(data.apply){
                   var ohtml ='<div class="fold"><a class="fold-a"id="newfriend"><div class="fold-title">有新朋友</div>有<span>'+data.apply+'</span>个人请求添加你为好友<i class="mui-icon mui-icon-arrowright"></i></a></div>'
                    $('.main').html(ohtml); 
                }
            }
            mui('.mui-scroll-wrapper').scroll({
                indicators: false, //是否显示滚动条 默认为true
                deceleration: 0.003, //阻尼系数,系数越小滑动越灵敏 默认0.0006
                bounce: true //是否启用回弹 默认true
            });
        }
    });
}


//获取我的屏蔽用户信息
function myphone() {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/getUsersShieldList",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token
        },
        success: function(data) {
            if(data.error_code == 0) {
                    for(var i = 0; i < data.data.length; i++) {
                        var su, sn = data.data[i].text;
                        data.data[i].text == '' ?su = '#':su = data.data[i].text;
                        ohtml += '<a class="title" id="' + sn.toLowerCase() + '">' + su + '</a>';
                        for(var j = 0; j < data.data[i].data.length; j++) {
                            ohtml += '<a class="userphone" data-id="' + data.data[i].data[j].id + '">'
                                   + '<img src="images/user_phone.png"/>' 
                                   +'<div class="mf-navigate-right">'
                                   +'<div class="user">' + data.data[i].data[j].shield_name + '</div>'
                                   +'<div class="phone">' + data.data[i].data[j].shield_mobile + '</div>'
                                   +'</div><i class="arrows cancel">取消屏蔽</i></a>';
                        }
                        
                    }
                $('.main').html(ohtml);
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                    exitlogin();
                },1000);
            } else {

            }
            mui('.mui-scroll-wrapper').scroll({
                indicators: false, //是否显示滚动条 默认为true
                deceleration: 0.003, //阻尼系数,系数越小滑动越灵敏 默认0.0006
                bounce: true //是否启用回弹 默认true
            });
        }
    });
}

//获取取消屏蔽号码
function derphone(obj) {
    var id =$(obj).parents('.userphone').attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/shieldCancel",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            id:id
        },
        success: function(data) {
            if(data.error_code == 0) {
                if($(obj).parents('.userphone').prev('a').hasClass('title') && ($(obj).parents('.userphone').next('a').hasClass('title')|| !$(obj).parents('.userphone').next().length)  ){
                    $(obj).parents('.userphone').prev('a').remove();
                }
                $(obj).parents('.userphone').remove();
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                    exitlogin();
                },1000);
            } else {
                mui.toast('取消屏蔽失败！');
            }
        }
    });
}

//获取我的屏蔽朋友圈用户信息
function myshield() {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/getUsersBlackList",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token
        },
        success: function(data) {
            if(data.error_code == 0) {
                    for(var i = 0; i < data.data.length; i++) {
                        var su, sn = data.data[i].text;
                        data.data[i].text == '' ?su = '#':su = data.data[i].text;
                        ohtml += '<a class="title" id="' + sn.toLowerCase() + '">' + su + '</a>';
                        for(var j = 0; j < data.data[i].data.length; j++) {
                            ohtml += '<a class="shield" data-id="' + data.data[i].data[j].uid + '">'
                                   + '<img src="'+DOMAIN_NAME+data.data[i].data[j].face+'" onerror="imgExists(this)" />' 
                                   +'<div class="mf-navigate-right">'
                                   +'<div class="user">' + data.data[i].data[j].nickname + '</div>'
                                   +'</div><i class="arrows show">显示</i></a>';
                        }
                        
                    }
                $('.main').html(ohtml);
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                        exitlogin();
                    },1000);
            } else {

            }
            mui('.mui-scroll-wrapper').scroll({
                indicators: false, //是否显示滚动条 默认为true
                deceleration: 0.003, //阻尼系数,系数越小滑动越灵敏 默认0.0006
                bounce: true //是否启用回弹 默认true
            });
        }
    });
}

//显示我的屏蔽朋友圈用户
function dershield(obj) {
    var id =$(obj).parents('.shield').attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/blackCancel",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            friend_uid:id,
        },
        success: function(data) {
            if(data.error_code == 0) {
                if($(obj).parents('.shield').prev('a').hasClass('title') && ($(obj).parents('.shield').next('a').hasClass('title')|| !$(obj).parents('.shield').next().length)  ){
                    $(obj).parents('.shield').prev('a').remove();
                }
                $(obj).parents('.shield').remove();
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                        exitlogin();
                    },1000);
            } else {
                mui.toast('显示失败！');
            }
        }
    });
}


//删除好友
function delectfringd(othis) {
    var oname =othis.find('.user').text();
    var id = othis.attr('data-id');
    mui.confirm('确定删除好友'+oname+'?','提示',['确定','取消'],function(e){
        if(!e.index==1){
            $.ajax({
                type: "post",
                url: DOMAIN_NAME + "/index.php/api/friend/friendsDel",
                dataType: 'jsonp',
                data: {
                    uid: uid,
                    token: token,
                    friend_uid:id,
                },
                success: function(data) {
                    if(data.error_code == 0) {
                        if(othis.prev().hasClass('title')&&(othis.next().hasClass('title')||othis.next().length==0)){
                            othis.prev().remove();
                        }
                        othis.remove();
                    } else if(data.error_code == 1001) {
                        setTimeout(function() {
                            common.pageTransition("login.html");
                        });
                    } else {
                        mui.toast('显示失败！');
                    }
                }
            });
        }
    },'div');
}